import os

# 指定目录路径
directory = r"C:\Users\Anyphasy\Downloads\copy电影"

img_urls = []
moment_id = 1
i=0
temp_img_list=[]
# 遍历目录中的文件
for filename in os.listdir(directory):
    # 检查文件名是否以"u="开头并且以"JPEG"结尾
    if filename.startswith("u=") and filename.endswith("JPEG"):
        # 1. 添加前缀并输出
        """ url = "https://img2.baidu.com/it/" + filename
        print(url)

        temp_img_list.append(url)
        i+=1 """
        
#         if moment_id<11 and len(img_urls) == 5:
#             imgs = " ".join(img_urls)
#             # print(imgs)
#             # 拼接SQL语句
#             sql = f"""UPDATE graduate.moment 
# SET image_urls = '{imgs}' 
# WHERE moment_id = {moment_id};"""
#             print(sql)
#             moment_id += 1
#             img_urls.clear()
#    每5个URL生成一条UPDATE语句
#         if len(img_urls) == 5 and moment_id <= 10:
#             # 为每个URL添加双引号包裹，并用逗号分隔
#             csv_urls = ",".join([f'"{url}"' for url in img_urls])
            
#             # 生成带转义处理的SQL语句
#             sql = f"""UPDATE graduate.moment 
# SET image_urls = '{csv_urls}' 
# WHERE moment_id = {moment_id};"""
            
#             print(sql)
#             moment_id += 1
#             img_urls.clear()

        




        # 修改此处
        # 2. 修改文件后缀为.jpg
        # 获取文件完整路径
        old_path = os.path.join(directory, filename)
        
        # 分割文件名和扩展名(如果有)
        base_name = os.path.splitext(filename)[0]  # 获取不带扩展名的文件名
        new_filename = base_name + ".jpg"  # 添加.jpg后缀
        
        # 构建新路径
        new_path = os.path.join(directory, new_filename)
        
        # 重命名文件
        os.rename(old_path, new_path)
        print(f"已重命名: {filename} -> {new_filename}")

print("操作完成")
print('长度为',len(img_urls))